package com.tzxs.pay.main.mine;

import android.Manifest;
import android.annotation.SuppressLint;
import android.arch.lifecycle.ViewModelProviders;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.widget.Toast;

import com.tbruyelle.rxpermissions2.RxPermissions;
import com.tzxs.pay.R;
import com.tzxs.pay.databinding.FragmentMineBinding;
import com.tzxs.pay.main.BaseFragment;
import com.tzxs.pay.main.mine.cashier.CashierMangeActivity;
import com.tzxs.pay.main.mine.goods.GoodsManageActivity;
import com.tzxs.pay.main.mine.signing.SigningManageActivity;
import com.tzxs.pay.util.ISharedPreference;

public class MineFragment extends BaseFragment<FragmentMineBinding> {

    private static final String TAG = MineFragment.class.getSimpleName();
    private static final int CALLPHONE = 100;

    private RxPermissions rxPermissions;

    @Override
    public int getContentLayoutId() {
        return R.layout.fragment_mine;
    }

    @Override
    protected void viewCreate(Bundle savedInstanceState) {
        if (getActivity() == null)
            return;
        rxPermissions = new RxPermissions(getActivity());
        binding.userName.setText(ISharedPreference.getInstance(getActivity().getApplication()).getUserName());
        binding.userPhone.setText(ISharedPreference.getInstance(getActivity().getApplication()).getPhone());


        binding.setListener(new MineListener() {
            @Override
            public void intentGoodsManageActivity() {
                //商品管理
                Intent intent = new Intent(getActivity(), GoodsManageActivity.class);
                startActivity(intent);
            }

            @Override
            public void intentSigningManageActivity() {
                //签约管理
                Intent intent = new Intent(getActivity(), SigningManageActivity.class);
                startActivity(intent);
            }

            @Override
            public void intentCashierMangeActivity() {
                //收银员管理
                Intent intent = new Intent(getActivity(), CashierMangeActivity.class);
                startActivity(intent);
            }

            @Override
            public void intentSettingActivity() {
                //设置
                Intent intent = new Intent(getActivity(), SettingActivity.class);
                startActivity(intent);
            }

            @Override
            public void callService() {
                Log.d(TAG, "callService: 拨打客服电话");
                //拨打客服电话
                AlertDialog alertCall = new AlertDialog.Builder(getContext())
                        .setTitle("拨打电话")
                        .setMessage("是否拨打电话:" + binding.servicePhone.getText().toString().trim())
                        .setNegativeButton("确认", (dialog, which) -> rxPermissions
                                .requestEach(Manifest.permission.CALL_PHONE)
                                .subscribe(permission -> { // will emit 2 Permission objects
                                    if (permission.granted) {
                                        Log.d(TAG, "onClick: 进入打电话权限获取");

                                        callPhone(binding.servicePhone.getText().toString().trim());
                                    } else if (permission.shouldShowRequestPermissionRationale) {
                                        Toast.makeText(getContext(), "要拨打电话,请同意拨打电话权限!", Toast.LENGTH_SHORT).show();
                                    } else {
                                        Uri packageURI = Uri.parse("package:" + getContext().getPackageName());
                                        Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, packageURI);
                                        startActivity(intent);
                                    }
                                })).setPositiveButton("取消", (DialogInterface dialog, int which) -> {
                            dialog.dismiss();
                        })
                        .create();

                alertCall.show();

            }
        });
    }

    /**
     * 拨打电话
     *
     * @ phone 手机号
     */
    private void callPhone(String phone) {
        Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phone));
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }

}


